import auth_api from '../../api/auth_api';

const state = {
    userInfo: {},
    authMap: {},
}

const mutations = {
    setUserInfo(state, data) {
        state.userInfo = data
    },
    setAuthMap(state, data) {
        state.authMap = data
    },
}

const actions = {
    login(context, payload) {
        return new Promise((resolve, reject) => {
            auth_api.login(payload).then(data => {
                resolve(data)
            }).catch(err => {
                reject(err)
            })
        })
    },
    logout(context, payload) {
        return new Promise((resolve, reject) => {
            auth_api.logout().then(data => {
                context.commit('setUserInfo', {})
                resolve(data)
            }).catch(err => {
                reject(err)
            })
        })
    },
    setUserInfo(context, payload) {
        context.commit('setUserInfo', payload)
    },
    setAuthMap(context, payload) {
        context.commit('setAuthMap', payload)
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}